package karaoke.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import karaoke.excepcion.DAOExcepcion;
import karaoke.modelo.JsonResult;
import karaoke.modelo.Reserva;
import karaoke.negocio.GestionReserva;

/**
 * Servlet implementation class ConsultaReservaServlet
 */
@WebServlet("/ConsultaReservaServlet")
public class ConsultaReservaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ConsultaReservaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		Gson gson = new Gson();
		int idreserva = Integer.parseInt(request.getParameter("idreserva"));
		GestionReserva negocioR = new GestionReserva();
		JsonResult results = new JsonResult();
		
		Reserva objreserva = null;
		try {
			objreserva = negocioR.obtener(idreserva);
		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			results.addMenssage(JsonResult.ERROR, e.getMessage());
		}
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		if(objreserva!=null){
			out.println(gson.toJson(objreserva));
		}else{
			out.println(gson.toJson(results));
		}
	}

}
